
(define (words)
  (cons "mother" (cons "in" (cons "law" (cons "woman" (cons "hitler" (cons "debit" (cons "card" (cons "bad" (cons "credit" (cons "school" (cons "master" (cons "the" (cons "classroom" (cons "eleven" (cons "plus" (cons "two" (cons "twelve" (cons "plus" (cons "one" (cons "dormitory" (cons "dirty" (cons "room" (cons "punishment" (cons "nine" (cons "thumps" (cons "the" (cons "morse" (cons "code" (cons "here" (cons "come" (cons "dots" (cons "a" (cons "decimal" (cons "point" (cons "im" (cons "a" (cons "dot" (cons "in" (cons "place" (cons "astronomer" (cons "moon" (cons "starer" (cons "the" (cons "eyes" (cons "they" (cons "see" '())))))))))))))))))))))))))))))))))))))))))))))))

;(define (vector->list vec i)
;  (if (= i (vector-length vec))
;      '()
;      (cons (vector-ref vec i)
					;	    (vector->list vec (+ i 1)))))
(define (map f l)
  (if (null? l)
      '()
     (cons (f (car l))
           (map f (cdr l)))))

(define (append x y)
 (if (null? x)
     y
     (cons (car x) (append (cdr x) y))))

(define (concatenate lists)
  (if (null? lists)
      '()
      (append (car lists) (concatenate (cdr lists)))))

(define (sift-by-loop thing to-process yes no cont)
 (if (null? to-process)
     (cont yes no)
     (if (comparator thing (car to-process))
         (sift-by-loop thing (cdr to-process) (cons (car to-process) yes) no cont)
         (sift-by-loop thing (cdr to-process) yes (cons (car to-process) no) cont))))

(define (sift-by thing things cont)
 (sift-by-loop thing things (cons thing '()) '() cont))

(define (group-by things)
 (if (null? things)
     '()
     (sift-by (car things) (cdr things)
       (lambda (ys zs)
         (cons (cons (car things) ys)
               (group-by zs))))))

(define (filter pred list)
  (if (null? list)
      '()
      (if (pred (car list))
          (cons (car list) (filter pred (cdr list)))
          (filter pred (cdr list)))))

(define (comparator x y) (= (car x) (car y)))
(define (not-null? n) (not (null? n)))
(define (merge sequences)
  (map merge* (group-by (filter not-null? sequences))))
(define (merge* sequences)
  ;; invariant: all sequences have the same car
  (cons (car (car sequences))
        (cons (merge (map cdr sequences))
              '())))

(define (print-each list)
  (if (null? list)
      '()
     (begin (print (car list))
            (print-each (cdr list)))))

(define (length l)
 (if (null? l) 0 (+ 1 (length (cdr l)))))
(define (list-ref l i)
 (if (= 0 i) (car l) (list-ref (cdr l) (- i 1))))
;;(define (list->vector l)
;; (make-vector (length l) (lambda (i) (list-ref l i))))

(define (print-each-word list)
  (if (null? list)
      '()
      (begin (begin (print (list->vector (car list)))
		    (print "\n"))
	     (print-each-word (cdr list)))))

(define (go tree)
  (map (lambda (ord) (cons (car tree) ord))
       (og (car (cdr tree)))))

(define (og trees)
 (if (null? trees)
     (cons '() '())
     (concat-map go trees)))

(define (concat-map f l)
  (concatenate (map f l)))

(define scm-main
  (print-each-word
   (og
    (merge (map (lambda (w) (map char->integer (string->list w))) (words))))))
